/*
 * Copyright 2019-2025 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * https://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.iiifi.kite.captcha;

import java.util.concurrent.ThreadLocalRandom;

import org.springframework.cache.Cache;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import com.iiifi.kite.captcha.utils.CaptchaUtils;
import com.iiifi.kite.core.util.Base64Utils;
import com.iiifi.kite.core.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * BaseCaptcha
 *
 * @author ii@iiifi.com 花朝
 */
@Slf4j
public abstract class BaseCaptcha {

    private static final String DEFAULT_COOKIE_NAME = "kite-captcha";
    private static final String DEFAULT_CHACHE_NAME = "kiteCaptchaCache";

    /**
     * cookie超时默认为session会话状态
     */
    protected final static int DEFAULT_MAX_AGE = -1;

    private String cacheName;
    private String cookieName;
    public Cache captchaCache;

    public BaseCaptcha(Cache captchaCache) {
        this.cacheName = DEFAULT_CHACHE_NAME;
        this.cookieName = DEFAULT_COOKIE_NAME;
        this.captchaCache = captchaCache;
    }

    public String getCacheName() {
        return cacheName;
    }

    public void setCacheName(String cacheName) {
        this.cacheName = cacheName;
    }

    public String getCookieName() {
        return cookieName;
    }

    public void setCookieName(String cookieName) {
        this.cookieName = cookieName;
    }

    /**
     * 图片输出的头，避免验证码被缓存
     *
     * @return HttpHeaders
     */
    protected HttpHeaders getResponseHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setPragma("no-cache");
        headers.setCacheControl("no-cache");
        headers.setExpires(0);
        headers.setContentType(MediaType.IMAGE_JPEG);
        return headers;
    }

    /**
     * 生成验证码-Base64
     *
     * @return {String}
     */
    public Captcha generateBase64() {
        return generate(true);
    }

    /**
     * 生成验证码
     *
     * @return {String}
     */
    public Captcha generate() {
        return generate(false);
    }

    private Captcha generate(boolean isBase64) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        // 转成大写重要
        String captchaCode = CaptchaUtils.generateCode(random).toUpperCase();
        // 生成验证码
        byte[] imgBytes = CaptchaUtils.generate(random, captchaCode);
        String uuid = StringUtils.getUUID();
        // 保存验证码缓存
        captchaCache.put(uuid, captchaCode);
        if (isBase64) {
            return new Captcha(uuid, Base64Utils.encodeToString(imgBytes));
        } else {
            return new Captcha(uuid, imgBytes);
        }
    }

    /**
     * 校验验证码
     *
     * @param uuid uuid
     * @param userInputCaptcha 用户输入的验证码
     * @return 是否成功
     */
    public boolean validate(String uuid, String userInputCaptcha) {
        if (log.isInfoEnabled()) {
            log.info("validate captcha userInputCaptcha is {}", userInputCaptcha);
        }
        String captchaCode = captchaCache.get(uuid, String.class);
        if (StringUtils.isBlank(captchaCode)) {
            return false;
        }
        // 转成大写重要
        userInputCaptcha = userInputCaptcha.toUpperCase();
        boolean result = userInputCaptcha.equals(captchaCode);
        if (result) {
            // 校验成功删除缓存
            captchaCache.evict(uuid);
        }
        return result;
    }
}
